/**
 * 
 */
package mdb.com.vn.persist;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.stereotype.Repository;

import mdb.com.vn.model.FixedDeposits;

/**
 * @author tu.dod
 *
 */
@Repository("fixedDepositsDAO")
public class FixedDepositsDAOImpl extends AbstractHibernateDAOImpl<FixedDeposits,Integer> implements FixedDepositsDAO,Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDeposits> findByPersionalType(int type) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDeposits.findByPersionalType").setInteger("customerType", type).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDeposits> findByCorporateType(int type) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDeposits.findByPersionalType").setInteger("customerType", type).list();
	}

	@Override
	protected Class<FixedDeposits> getDomainClass() {
		// TODO Auto-generated method stub
		return FixedDeposits.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDeposits> findByCode(String code) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDeposits.findByCode").setString("savingbookNo", code).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getSCardFDAvailable(String customerRef) {
		// TODO Auto-generated method stub
		int totalSCAvailable = 0;
		List<FixedDeposits> deposits = getSession().getNamedQuery("FixedDeposits.findByCustomerRef").setString("customerId", customerRef).list();
		for (FixedDeposits fixedDeposits : deposits) {
			totalSCAvailable += fixedDeposits.getScCardNotGet();
		}
		return totalSCAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDeposits> findByCustomerRef(String customerId) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDeposits.findByCustomerRef").setString("customerId", customerId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public double getAmountCorpAvailable(String customerRef) {
		// TODO Auto-generated method stub
		double totalSCAvailable = 0;
		List<FixedDeposits> deposits = getSession().getNamedQuery("FixedDeposits.findByCustomerRef").setString("customerId", customerRef).list();
		for (FixedDeposits fixedDeposits : deposits) {
			totalSCAvailable += fixedDeposits.getScCardAmtNot();
			}
		return totalSCAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getSCardFDAvailableException(String customerRef,
			String accountException) {
		int totalSCAvailable = 0;
		List<FixedDeposits> deposits = getSession().getNamedQuery("FixedDeposits.findByCustomerRef").setString("customerId", customerRef).list();
		for (FixedDeposits fixedDeposits : deposits) {
			if(!fixedDeposits.getSavingbookNo().equals(accountException)){
				totalSCAvailable += fixedDeposits.getScCardNotGet();
			}
		}
		return totalSCAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDeposits> getAllFixexDeposits() {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDeposits.getAllFixedDeposits").list();
	}

	@Override
	public double getAmountCorpAvailableException(String customerRef,
			String accountException) {
		// TODO Auto-generated method stub
		int totalSCAvailable = 0;
		@SuppressWarnings("unchecked")
		List<FixedDeposits> deposits = getSession().getNamedQuery("FixedDeposits.findByCustomerRef").setString("customerId", customerRef).list();
		for (FixedDeposits fixedDeposits : deposits) {
			if(!fixedDeposits.getSavingbookNo().equals(accountException)){
				totalSCAvailable += fixedDeposits.getScCardNotGet();
			}
			
		}
		return totalSCAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countThanksCasrd() {
		// TODO Auto-generated method stub
		List<Long> longs = getSession().getNamedQuery("FixedDeposits.countThanksCard").list();
		if (longs.size() > 0){
			if(longs.get(0)!=null){
				return longs.get(0).intValue();
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BigDecimal sumAmount() {
		// TODO Auto-generated method stub
		List<BigDecimal> longs = getSession().getNamedQuery("FixedDeposits.sumAmount").list();
		if (longs.size() > 0){
			if(longs.get(0)!=null){
				//return new BigDecimal(longs.get(0).toString()) ;
				return longs.get(0);
			}
		}
		return new BigDecimal(0);
	}

}
